﻿
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Tetris.Interfaces;

namespace Tetris.Blocks
{
    // ReSharper disable InconsistentNaming
    public class BarBlock : AbstractBlock
    // ReSharper restore InconsistentNaming
    {
        // ----
        private readonly ITile _tileLeft;
        private readonly ITile _tileMidLeft;
        private readonly ITile _tileMidRight;
        private readonly ITile _tileRight;

        public BarBlock(Texture2D texture, Color color, Vector2 position, int fallDelay)
        {
            Texture = texture;
            Color = color;
            Tiles = new List<ITile>();

            // setup skeleton
            _tileLeft = new Tile(Texture, Color);
            _tileMidLeft = new Tile(Texture, Color);
            _tileMidRight = new Tile(Texture, Color);
            _tileRight = new Tile(Texture, Color);
            Tiles.AddRange(new[] { _tileLeft, _tileMidLeft, _tileMidRight, _tileRight });

            Position = new Vector2(position.X, position.Y);
            FallDelay = fallDelay;
            LastFallen = 0;
        }

        protected override void ReArrangeParts(Vector2 position, RotationAngle angle)
        {
            if (angle == RotationAngle.South || angle == RotationAngle.North)
            {
                _tileLeft.Position = new Vector2(position.X - Texture.Width, position.Y);
                _tileMidLeft.Position = new Vector2(position.X, position.Y);
                _tileMidRight.Position = new Vector2(position.X + Texture.Width, position.Y);
                _tileRight.Position = new Vector2(position.X + Texture.Width*2, position.Y);
            }
            else 
            {
                _tileLeft.Position = new Vector2(position.X, position.Y + Texture.Height);
                _tileMidLeft.Position = new Vector2(position.X, position.Y);
                _tileMidRight.Position = new Vector2(position.X, position.Y - Texture.Height);
                _tileRight.Position = new Vector2(position.X, position.Y - Texture.Height*2);
            }
        }

        public override void Rotate()
        {
            CurrentAngle = GetNextRotationAngle();
            ReArrangeParts(Position, CurrentAngle);
        }
    }
}
